<template>
  <div>
    <el-dialog
      title="新增员工"
      :visible.sync="dialogVisible"
      width="650px"
      @close="closeEvent"
    >
      <el-form ref="form" label-width="120px" :model="form" :rules="rules">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username" placeholder="1-30个字符" />
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            v-model="form.timeOfEntry"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="聘用形势" prop="formOfEmployment">
          <el-select v-model="form.formOfEmployment" placeholder="请选择">
            <el-option
              v-for="item in employeesData.hireType"
              :key="item.id"
              :label="item.value"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="form.workNumber" placeholder="1-20个字符" />
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <el-input
            v-model="form.departmentName"
            placeholder="1-20个字符"
            readonly
            @focus="focusEvent"
          />
          <el-tree
            v-if="showTree"
            v-loading="loading"
            :data="departmentList"
            :props="{ label: 'name' }"
            @node-click="nodeClick"
          />
          <!-- <el-tree>展示组织结构</el-tree> -->
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker
            v-model="form.correctionTime"
            type="date"
            placeholder="选择日期"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: center">
          <el-button @click="closeEvent">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import employeesData from '@/api/constant/employees.js'
import { companyDepartment } from '@/api/departments.js'
import { changeData } from '@/utils/index.js'
import { checkPhone } from '@/utils/validate'
import { sysUserPost } from '@/api/employees.js'
export default {
  data() {
    return {
      loading: false,
      showTree: false,
      employeesData: employeesData,
      dialogVisible: false,
      departmentList: [],
      form: {
        username: '', // string	非必须		姓名
        mobile: '', // string	非必须		手机号
        timeOfEntry: '', // string	非必须		入职时间
        formOfEmployment: '', // number	非必须		聘用形式
        workNumber: '', // string	非必须		工号
        departmentName: '', // string	非必须		部门
        correctionTime: '' // string	非必须		转正时间
      },
      rules: {
        username: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            min: 1,
            max: 30,
            message: '请输入1-30个字符',
            trigger: 'change'
          }
        ],
        mobile: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            validator: (vule, value, callback) => {
              checkPhone(value)
                ? callback()
                : callback(new Error('请输入正确的手机号'))
            },
            trigger: 'change'
          }
        ],
        timeOfEntry: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        formOfEmployment: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ],
        workNumber: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            min: 1,
            max: 20,
            message: '请输入1-20个字符',
            trigger: 'change'
          }
        ],
        departmentName: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            min: 1,
            max: 20,
            message: '请输入1-20个字符',
            trigger: 'change'
          }
        ],
        correctionTime: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ]
      }
    }
  },
  methods: {
    async focusEvent() {
      this.showTree = true // 显示el-tree
      // 获取组织架构数据并转换成树形结构
      // 没数据才调用接口
      if (this.departmentList.length === 0) {
        this.loading = true
        // 获取组织架构数据并转换成树形结构
        const res = await companyDepartment()
        this.loading = false
        // 聚焦时获取数据并转换成树形结构 并渲染
        this.departmentList = changeData(res.data.depts, '') // 存储
        console.log('组织结构', this.departmentList)
      }
    },
    nodeClick(row) {
      this.form.departmentName = row.name
      this.showTree = false
      console.log(row)
    },
    // 确定按钮点击
    submit() {
      // 确定时表单验证
      this.$refs.form.validate(async(result) => {
        if (result) {
          // 新增api
          await sysUserPost(this.form)
          // 调用完成 1.提示 2.关闭弹框 3.刷新父级列表数据 4.清空与表单验证
          this.$message.success('新增成功')
          this.$emit('getData')
          this.closeEvent()
          console.log(result)
        }
      })
    },
    closeEvent() {
      this.dialogVisible = false
      // 重置表单数据与清空表单验证
      this.$refs.form.resetFields()
      this.form = {
        username: '', // string	非必须		姓名
        mobile: '', // string	非必须		手机号
        timeOfEntry: '', // string	非必须		入职时间
        formOfEmployment: '', // number	非必须		聘用形式
        workNumber: '', // string	非必须		工号
        departmentName: '', // string	非必须		部门
        correctionTime: '' // string	非必须		转正时间
      }
    }
  }
}
</script>

<style></style>
